﻿#region Using Directives

using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TeamArchitect.PowerTools;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;

#endregion

namespace FeatureBuilder.ContribLib.Commands
{
    [DisplayName("Execute Command")]
    [Description("Executes a Command.")]
    [Category("IDE")]
    public class ExecuteCommand : FeatureCommand
    {
        public string CommandName { get; set; }
        public string CommandArgs { get; set; }

        /// <summary>
        /// Gets the ServiceProvider that we will use to get a reference to DTE.
        /// </summary>
        [Import(typeof(SVsServiceProvider))]
        public IServiceProvider ServiceProvider { get; private set; }

        /// <summary>
        /// Gets and sets the UriReferenceService using MEF
        /// The UriReferenceService takes, amongst other things, content:// URIs
        /// and can retrieve either the path or contents of the specified file as desired
        /// </summary>
        [Import]
        public IFxrUriReferenceService UriReferenceService { get; set; }

        /// <summary>
        /// Gets the Feature Extension instance for this invocation
        /// of the Condition
        /// </summary>
        [Import(AllowDefault = true)]
        public IFeatureExtension Feature { get; private set; }

        #region Overrides

        public override void Execute()
        {
            var dte = ServiceProvider.GetService(typeof(DTE)) as EnvDTE.DTE;

            if (dte == null)
                return;

            dte.StatusBar.Text = string.Format("Executing command {0} {1}", CommandName, CommandArgs);
            if (CommandArgs != null)
            {
                dte.ExecuteCommand(CommandName, CommandArgs);
            }
            else
            {
                dte.ExecuteCommand(CommandName);
            }
            dte.StatusBar.Text = "Ready";
        }

        #endregion
    }
}